package com.welab.springcloud.demo.common.interceptor;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.MDC;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Enumeration;


@Slf4j
@Aspect
public class WebLogAspect {

    @Pointcut("@within(org.springframework.web.bind.annotation.RestController)")
    public void webLog() {
    }

    @Before("webLog()")
    public void doBefore(JoinPoint joinPoint) {

        MDC.put("time", System.currentTimeMillis()+"");
        log.info("WebLogAspect.doBefore()");
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();

        // 记录下请求内容
        log.info("请求URL : " + request.getRequestURL().toString());
        log.info("请求方法 : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
        log.info("请求参数 : " + Arrays.toString(joinPoint.getArgs()));

        //获取所有参数方法一：
        Enumeration<String> enu = request.getParameterNames();
        while (enu.hasMoreElements()) {
            String paraName = enu.nextElement();
            log.info(paraName + ": " + request.getParameter(paraName));
        }
    }

    @AfterReturning(value = "webLog()", returning = "resultMap")
    public void doAfterReturning(JoinPoint joinPoint, Object resultMap) {
        // 处理完请求，返回内容
        log.info("WebLogAspect.doAfterReturning()");
        String json = JSON.toJSONString(resultMap);
        if(!StringUtils.isEmpty(json) && json.length() > 1000) {
            json = json.substring(0, 1000);
        }
        log.info("返回结果：{}", json);
        log.info("耗时（毫秒） : " + (System.currentTimeMillis() - Long.parseLong(MDC.get("time"))));
        MDC.remove("time");
    }

}